/***************************************************************************************************
                                项目                :       GimKeyboard
                                处理器				:       ESP32-S3
                                开发环境			:       pio(Arduino)
                                文件				:       hc165_keyboard_matrix_drv.h
                                说明				:       74HC165驱动头文件
                                作者				:       金欣嵘
====================================================================================================

====================================================================================================
                                               修改记录 
====================================================================================================
----------------------------------------------------------------------------------------------------

                                               2024.02.04
----------------------------------------------------------------------------------------------------
                            修改人                  :            金欣嵘
                            修改记录                :            创建文件 
----------------------------------------------------------------------------------------------------
***************************************************************************************************/
#ifndef __HC165_KEYBOARD_MATRIX_DEV_H__
#define __HC165_KEYBOARD_MATRIX_DEV_H__

#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <timers.h>

#define KEYBOARD_MATRIX_DEBUG                   0                               //是否打印调试信息

#define KEYBOARD_MATRIX_DRV_TASK_STACK_SIZE     2048                            //任务堆栈大小
#define KEYBOARD_MATRIX_DRV_TASK_PRIO           10                              //任务优先级

#define KEYBOARD_MATRIX_DRV_QUEUE_LEN           255                             //队列长度

#define KEYBOARD_PUSH                           1                               //按键被按下
#define KEYBOARD_RELEASE                        0                               //按键被释放

#define HC165_NUM                               11                              //共多少个HC165组成
#define HC165_DATA_PIN_NUM                      8                               //一个芯片可以扫描多少引脚

#define KEYBOARD_KEY_ESC                        7
#define KEYBOARD_KEY_F1                         6
#define KEYBOARD_KEY_F2                         5
#define KEYBOARD_KEY_F3                         4
#define KEYBOARD_KEY_F4                         3
#define KEYBOARD_KEY_F5                         2
#define KEYBOARD_KEY_F6                         1
#define KEYBOARD_KEY_F7                         0
#define KEYBOARD_KEY_F8                         8
#define KEYBOARD_KEY_F9                         9
#define KEYBOARD_KEY_F10                        10
#define KEYBOARD_KEY_F11                        11
#define KEYBOARD_KEY_F12                        12
#define KEYBOARD_KEY_PRINT_SCREEN               13
#define KEYBOARD_KEY_SCROLL_LOCK                14
#define KEYBOARD_KEY_PAUSE_BREAK                15
#define KEYBOARD_KEY_TILDE                      23
#define KEYBOARD_KEY_1                          22
#define KEYBOARD_KEY_2                          21
#define KEYBOARD_KEY_3                          20
#define KEYBOARD_KEY_4                          19
#define KEYBOARD_KEY_5                          18
#define KEYBOARD_KEY_6                          17
#define KEYBOARD_KEY_7                          16
#define KEYBOARD_KEY_8                          31
#define KEYBOARD_KEY_9                          30
#define KEYBOARD_KEY_0                          29
#define KEYBOARD_KEY_MINUS_SIGN                 28
#define KEYBOARD_KEY_PLUS_SIGN                  27
#define KEYBOARD_KEY_BACKSPACE                  26
#define KEYBOARD_KEY_INSERT                     25
#define KEYBOARD_KEY_HOME                       24
#define KEYBOARD_KEY_PAGE_UP                    39
#define KEYBOARD_KEY_TAB                        38
#define KEYBOARD_KEY_Q                          37
#define KEYBOARD_KEY_W                          36
#define KEYBOARD_KEY_E                          35
#define KEYBOARD_KEY_R                          34
#define KEYBOARD_KEY_T                          33
#define KEYBOARD_KEY_Y                          32
#define KEYBOARD_KEY_U                          47
#define KEYBOARD_KEY_I                          46
#define KEYBOARD_KEY_O                          45
#define KEYBOARD_KEY_P                          44
#define KEYBOARD_KEY_FRONT_QUOTE                43
#define KEYBOARD_KEY_BACK_QUOTE                 42
#define KEYBOARD_KEY_CAESURA_SIGN               41
#define KEYBOARD_KEY_DELETE                     40
#define KEYBOARD_KEY_END                        55
#define KEYBOARD_KEY_PAGE_DOWN                  54
#define KEYBOARD_KEY_CAPS_LOCK                  53
#define KEYBOARD_KEY_A                          52
#define KEYBOARD_KEY_S                          51
#define KEYBOARD_KEY_D                          50
#define KEYBOARD_KEY_F                          49
#define KEYBOARD_KEY_G                          48
#define KEYBOARD_KEY_H                          63
#define KEYBOARD_KEY_J                          62
#define KEYBOARD_KEY_K                          61
#define KEYBOARD_KEY_L                          60
#define KEYBOARD_KEY_COLON                      59
#define KEYBOARD_KEY_QUOTATION_MARK             58
#define KEYBOARD_KEY_ENTER                      57
#define KEYBOARD_KEY_LEFT_SHIFT                 56
#define KEYBOARD_KEY_Z                          71
#define KEYBOARD_KEY_X                          70
#define KEYBOARD_KEY_C                          69
#define KEYBOARD_KEY_V                          68
#define KEYBOARD_KEY_B                          67
#define KEYBOARD_KEY_N                          66
#define KEYBOARD_KEY_M                          65
#define KEYBOARD_KEY_COMMA                      64
#define KEYBOARD_KEY_PERIOD                     79
#define KEYBOARD_KEY_QUESTION_MARK              78
#define KEYBOARD_KEY_RIGHT_SHIFT                77
#define KEYBOARD_KEY_UP_ARROW                   76
#define KEYBOARD_KEY_LEFT_CTRL                  75
#define KEYBOARD_KEY_LEFT_WIN                   74
#define KEYBOARD_KEY_LEFT_ALT                   73
#define KEYBOARD_KEY_SPACE                      72
#define KEYBOARD_KEY_RIGHT_ALT                  87
#define KEYBOARD_KEY_FN                         86
#define KEYBOARD_KEY_RIGHT_CLICK                85
#define KEYBOARD_KEY_RIGHT_CTRL                 84
#define KEYBOARD_KEY_LEFT_ARROW                 83
#define KEYBOARD_KEY_DOWN_ARROW                 82
#define KEYBOARD_KEY_RIGHT_ARROW                81
#define KEYBOARD_KEY_INVALID                    80

class KeyboardMatrixDrv
{
    public:
        static SemaphoreHandle_t matrix_sacn_begin_semaphore;
        static QueueHandle_t keyboard_matrix_queue;
        static uint8_t trigger_matrix[HC165_NUM];
        static void init(void);
        static bool get_fn_key_state(void);

    private:
        static TaskHandle_t keyboard_matrix_task_handler;
        static void lock_data(void);
        static void read_data(void);
        static void start_keyboard_matrix_task(void *pvParameters);

};

extern KeyboardMatrixDrv keyboard_matrix_drv;

#endif
